"""
URL configuration for DRF project.

The urlpatterns list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/5.2/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls')) 
"""
from django.contrib import admin
from django.urls import path, include, re_path
from django.conf import settings
from django.conf.urls.static import static
from rest_framework.routers import DefaultRouter
from student.views import StudentViewSet
from clazz.views import ClazzViewSet
from score.views import ScoreViewSet
from user import views as user_views
from course.views import CourseManagementView, CourseClassDetailView, CourseStudentDetailView
from department.views import DepartmentManagementView
from teacher.views import TeacherManagementView
from user.views import test_page, test_csv
from upload_app.views import upload_picture, send_email,test_error
router = DefaultRouter()
router.register(r'student', StudentViewSet)
router.register(r'clazz', ClazzViewSet)
router.register(r'score', ScoreViewSet)


urlpatterns = [

    path('api/test_page/', test_page, name='test_page'),
    path('api/test_csv/', test_csv, name='test_csv'),
    path('api/upload_picture/', upload_picture, name='upload_picture'),
    path('api/send_email/', send_email, name='send_email'),

    path('api/send_email', send_email, name='send_email_no_slash'),
    
    path('api/test_error', test_error, name='test_error'),
    path('admin/', admin.site.urls),
    # API路由 - 统一使用api前缀
    path('api/', include(router.urls)),  # 学生和班级接口
    # 显式添加对不带斜杠URL的支持
    re_path(r'^api/clazz'
, ClazzViewSet.as_view({'get': 'list'})),
    re_path(r'^api/student'
, StudentViewSet.as_view({'get': 'list'})),
    path('api/userinfo', user_views.user_info, name='user_info'), # 用户信息接口
    path('api/logout', user_views.logout), # 退出登录接口
    # 不需要认证的接口
    path('api/register', user_views.register), # 注册接口
    path('api/login', user_views.login), # 登录接口
    # 课程管理接口
    path('api/course/management/', CourseManagementView.as_view(), name='course-management'),
    path('api/course/management/<int:course_id>/', CourseManagementView.as_view(), name='course-management-detail'),
    path('api/course/management/<int:course_id>/classes/', CourseClassDetailView.as_view(), name='course-class-detail'),
    path('api/course/management/<int:course_id>/classes/<int:class_id>/', CourseClassDetailView.as_view(), name='course-class-detail-specific'),
    path('api/course/management/<int:course_id>/classes/<int:class_id>/students/', CourseStudentDetailView.as_view(), name='course-student-detail'),
    path('api/course/management/<int:course_id>/classes/<int:class_id>/students/<int:student_id>/', CourseStudentDetailView.as_view(), name='course-student-detail-specific'),
    # 部门管理接口
    path('api/department/', DepartmentManagementView.as_view(), name='department-management'),
    path('api/department/<int:department_id>/', DepartmentManagementView.as_view(), name='department-management-detail'),
    # 教师管理接口
    path('api/teacher/', TeacherManagementView.as_view(), name='teacher-management'),
    path('api/teacher/<int:teacher_id>/', TeacherManagementView.as_view(), name='teacher-management-detail'),
]

# 配置媒体文件URL路由
urlpatterns+=static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)







# 添加静态文件服务（仅在开发模式下）
if settings.DEBUG:
    urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
    # 为STATICFILES_DIRS中的静态文件添加路由
    for static_dir in settings.STATICFILES_DIRS:
        urlpatterns += static('/assets/', document_root=static_dir)

from django.views.generic import TemplateView
urlpatterns += [re_path(r'^(?:.*)/?'
, TemplateView.as_view(template_name='index.html'))]
